package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.CategoryUsedAdapter;
import com.rh.bmw4s.bean.BaseBean;
import com.rh.bmw4s.bean.CategoryUsedBean;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.DealResult;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WantToBuyActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private CategoryUsedAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_want_to_buy);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.USED_SORT)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        BaseBean<CategoryUsedBean> bean = DealResult.getInstace().dealData(WantToBuyActivity.this, response, new TypeToken<BaseBean<CategoryUsedBean>>() {
                        }.getType());
                        if (bean == null) {
                            return;
                        }
                        List<CategoryUsedBean> beans = bean.getDatas();
                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(WantToBuyActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<CategoryUsedBean> beans) {
        mAdapter.addAll(beans);
        recyclerView.refresh();

    }


    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("我要买");
        titleRightOneBtn.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CategoryUsedAdapter(WantToBuyActivity.this);
        mAdapter.setDataList(new ArrayList<CategoryUsedBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(WantToBuyActivity.this)
                .setHeight(R.dimen.divider_height_6)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.app_bg1)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.refresh();
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i = new Intent(WantToBuyActivity.this, BuyCarActivity.class);
                i.putExtra("id", mAdapter.getDataList().get(position).getId());
                i.putExtra("name", mAdapter.getDataList().get(position).getCategoryname());
                startActivity(i);
            }
        });
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_btn:
                Intent intent = new Intent(WantToBuyActivity.this, SearchActivity.class);
                intent.putExtra("searchtype", 1);
                startActivity(intent);
                break;
        }
    }
}
